﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;

namespace SManage.LibExtension
{
    public static class SnmpSharpLibExtension
    {
        public static Int32 ToInt32(this ISnmpData snmpData)
        {
            if (snmpData == null) {
                return 0;
            }
            switch (snmpData.TypeCode) {
                case (SnmpType.TimeTicks):
                    return Convert.ToInt32(((TimeTicks)snmpData).ToUInt32());
                case (SnmpType.Integer32):
                    return Convert.ToInt32(((Integer32)snmpData).ToInt32());
                case (SnmpType.Counter32):
                    return Convert.ToInt32(((Counter32)snmpData).ToString());
                case (SnmpType.Gauge32):
                    return Convert.ToInt32(((Gauge32)snmpData).ToUInt32());
                case (SnmpType.Null):
                    return 0;
            }
            int result = 0;
            try { 
                result = Convert.ToInt32(snmpData);
            } catch {
                result = 0;
            }
            return result;
        }

        public static Int64 ToInt64(this ISnmpData snmpData) {
            if (snmpData == null) {
                return 0;
            }
            switch (snmpData.TypeCode) {
                case (SnmpType.TimeTicks):
                    return Convert.ToInt64(((TimeTicks)snmpData).ToUInt32());
                case (SnmpType.Integer32):
                    return Convert.ToInt64(((Integer32)snmpData).ToInt32());
                case (SnmpType.Counter32):
                    return Convert.ToInt64(((Counter32)snmpData).ToString());
                case (SnmpType.Gauge32):
                    return Convert.ToInt64(((Gauge32)snmpData).ToUInt32());
                case (SnmpType.Null):
                    return 0;
            }
            Int64 result = Convert.ToInt64(snmpData);
            return result;
        }

        public static ISnmpData OidValue(this IList<Variable> variables, string oid) {
            if (variables == null) {
                return null;            
            }
            var id = new ObjectIdentifier(oid);
            var result = variables.Where(v => v.Id == id);
            if (result.Count() < 1) {
                return null;
            }
            return result.First().Data;
        }

        //[Obsolete("Please switch to SManage.TraceEngine.EngineExtension.WalkFromSyncItem()")]
        public static IList<Variable> WalkFromIPAddress(this ObjectIdentifier oid, IPAddress ip)
        {
            IList<Variable> variables = new List<Variable>();
            for (Int32 i = 0; i < 5; i++) {
                try {
                    Messenger.Walk(
                        VersionCode.V2,
                        new IPEndPoint(ip, 161),
                        new OctetString("public"),
                        oid,
                        variables,
                        10000,
                        WalkMode.WithinSubtree);
                } catch {
                    variables = null;
                }
                if (variables != null) {
                    break;
                }
            }
            return variables;

        }

        public static IList<Variable[]> ToTable(this IList<Variable> variables, int itemCount) {
            if (variables == null) {
                return null;
            }
            int rowCount = variables.Count / itemCount;
            var result = new List<Variable[]>();
            for (int i = 0; i < rowCount; i++) {
                var variable = new Variable[itemCount];
                for (int j = 0; j < itemCount; j++) {
                    variable[j] = variables[j * rowCount + i];
                }
                result.Add(variable);
            }
            return result;
        }
    }
}
